package com.jackrain.nea.fc.vp.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.utils.Constants;
import com.jackrain.nea.fc.utils.ObjectToUtil;
import com.jackrain.nea.fc.vp.api.ViewCvipQueryCmd;
import com.jackrain.nea.fc.vp.mapper.ViewCvipMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.QueryCmd;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author xuxinshuai
 * @date 2017/11/13
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "vp")
public class ViewCvipQueryCmdImpl extends CommandAdapter implements ViewCvipQueryCmd {

    @Reference(version = "2", group = "cp")
    private QueryCmd queryCmd;

    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();

        ViewCvipMapper viewCvipMapper = ApplicationContextHandle.getBean(ViewCvipMapper.class);
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        if ((null == param) || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }
        log.info("ViewCvipQueryCmdImpl--param-->{}",param);
//        log.debug("----------VIP档案查询参数-----------" + param);

        List<HashMap> mapList = new ArrayList<>();
        mapList = viewCvipMapper.selectvipsql(param.getJSONArray("IDS").toArray());
        JSONArray jsonArray = listToJsonarray(mapList);
        JSONArray toF4Array = new JSONArray();
        for (int i = 0; i < jsonArray.size(); i++) {

            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONObject toF4Jo = new JSONObject();

            //根据ID和类型ID获得帐户表的卡号ID
            Long vipid = jsonObject.getLong("ID");
            Long vipGropuID = viewCvipMapper.selecttypegroupid("FF");
            String cardid = viewCvipMapper.selectvipaccecode(vipid, vipGropuID);
            toF4Jo.put("ECODE", cardid);
            toF4Jo.put("VP_C_VIPTYPEGROUP_ID", "FF");
            toF4Jo.put("ENAME", jsonObject.getString("ENAME"));
            String vipsex = jsonObject.getString("SEX");
            if (!ObjectToUtil.isNullOREmpty(vipsex)) {
                String sex = jsonObject.getString("SEX");
                if ("M".equals(sex)) {
                    toF4Jo.put("SEX", 1);
                } else if ("W".equals(sex)) {
                    toF4Jo.put("SEX", 2);
                } else if ("X".equals(sex)) {
                    toF4Jo.put("SEX", 0);
                }
            } else {
                toF4Jo.put("SEX", "");
            }
            String birthdate = jsonObject.getString("BIRTHDATE");
            if (!ObjectToUtil.isNullOREmpty(birthdate)) {
                Date scoreDate = TypeUtils.castToDate(jsonObject.getString("BIRTHDATE"));
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String sc = simpleDateFormat.format(scoreDate);
                toF4Jo.put("BIRTHDATE", sc);
            } else {
                toF4Jo.put("BIRTHDATE", "");
            }
            toF4Jo.put("MOBIL", jsonObject.getString("MOBIL"));
            Long storeid = viewCvipMapper.selectcpstoreid(cardid, vipGropuID, vipid);
            if (storeid != null) {
                HashMap hashMap = new HashMap();
                hashMap.put("tableName", "CP_C_STORE");
                hashMap.put("fields", "ECODE");
                JSONObject whereJo = new JSONObject();
                whereJo.put("ID", storeid);
                hashMap.put("whereKeys", whereJo);
                hashMap.put("type", HashMap.class);
                HashMap result = (HashMap) queryCmd.queryTable(hashMap);
                if (result != null) {
                    String stroeecode = (String) result.get("ECODE");
                    toF4Jo.put("CP_C_STORE_ID", stroeecode);
                }
            }
            toF4Jo.put("ADDRESS", jsonObject.getString("ADDRESS"));
            toF4Jo.put("JOB", jsonObject.getString("JOB"));
            toF4Jo.put("REMARK", jsonObject.getString("REMARK"));

            String receive = jsonObject.getString("ISRECEIVEPROM");
            if (!ObjectToUtil.isNullOREmpty(receive)) {
                String isreceive = jsonObject.getString("ISRECEIVEPROM");
                if ("N".equals(isreceive)) {
                    toF4Jo.put("ISRECEIVEPROM", 0);

                } else if ("Y".equals(isreceive)) {
                    toF4Jo.put("ISRECEIVEPROM", 1);
                }
            } else {
                toF4Jo.put("ISRECEIVEPROM", "");
            }
            toF4Jo.put("RECEIVEWAY", jsonObject.getString("RECEIVEWAY"));
            toF4Jo.put("WECHAT", jsonObject.getString("WECHAT"));
            toF4Jo.put("EMAIL", jsonObject.getString("EMAIL"));
            toF4Jo.put("OPENID", jsonObject.getString("OPENID"));
            toF4Jo.put("ID_CARD", jsonObject.getString("ID_CARD"));
            toF4Jo.put("ACADEMIC", jsonObject.getString("ACADEMIC"));
            String isactive = viewCvipMapper.selectvipaccactive(vipid, vipGropuID);
            if (!ObjectToUtil.isNullOREmpty(isactive)) {
                if ("Y".equals(isactive)) {
                    toF4Jo.put("ISACTIVE", 1);
                } else if ("N".equals(isactive)) {
                    toF4Jo.put("ISACTIVE", 2);
                }
            } else {
                toF4Jo.put("ISACTIVE", "");
            }
//            toF4Jo.put("MODIFIERID", "INTER99999");
//            toF4Jo.put("MODIFIEDDATE", jsonObject.getDate("MODIFIEDDATE"));
            toF4Array.add(toF4Jo);
        }

        vh.put("data", toF4Array);
        vh.put("code", 0);
        vh.put("message", Resources.getMessage("success"));
        return vh;

    }

    /**
     * list转jsonarray
     *
     * @param mapList
     * @return jsonArray
     */
    public JSONArray listToJsonarray(List<HashMap> mapList) {
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < mapList.size(); i++) {
            HashMap map = (HashMap) mapList.get(i);
            JSONObject jsonObject = new JSONObject(map);
            JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(jsonObject, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            jsonArray.add(param);
        }
        return jsonArray;
    }
}